package com.uploadFile.controller;

import com.uploadFile.config.FaceConfig;
import com.uploadFile.config.OssConfig;
import com.uploadFile.entity.dto.FileUploadDTO;
import com.uploadFile.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Author pl711@qq.com
 * @PackageName uploadFile
 * @Package com.uploadFile.controller
 * @Date 2024/5/13 18:59
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("face")
public class FaceController {
    @Autowired
    private OssConfig ossConfig;
    @Autowired
    private FaceConfig faceConfig;

    // 人脸融合
    @ApiOperation(value = "人脸融合")
    @PostMapping("upload/face")
    public Result<FileUploadDTO> uploadDTOResult(
            @RequestParam("face") MultipartFile face,
            @RequestParam("template") MultipartFile template,
            @RequestParam("fileType") String fileType
    ) throws Exception {
        String facePath = ossConfig.uploadFile(face, fileType);
        String templatePath = ossConfig.uploadFile(template, fileType);
        facePath = "https://"+ossConfig.getBucketName()+"."+ossConfig.getEndpoint()+"/"+facePath;
        Map map = faceConfig.faceMerge(facePath, templatePath);
        return Result.success(map);
    }

    @ApiOperation(value = "人物动漫化")
    @PostMapping("upload/Anime")
    public Result<FileUploadDTO> animeResult(
            @RequestParam("face") MultipartFile face,
            @RequestParam("fileType") String fileType
    ) throws Exception {
        String facePath = ossConfig.uploadFile(face, fileType);
        facePath = "https://"+ossConfig.getBucketName()+"."+ossConfig.getEndpoint()+"/"+facePath;
        log.info("facePath:{}",facePath);
        Map map = faceConfig.generateHumanAnimeStyleWithOptions(facePath);
        return Result.success(map);
    }

    //人脸对比 1:1
    @ApiOperation(value = "人脸对比")
    @PostMapping("upload/faceCompare")
    public Result<FileUploadDTO> faceCompareResult(
            @RequestParam("face") MultipartFile face,
            @RequestParam("template") MultipartFile template,
            @RequestParam("fileType") String fileType
    ) throws Exception {
        String facePath = ossConfig.uploadFile(face, fileType);
        String templatePath = ossConfig.uploadFile(template, fileType);
        facePath = "https://"+ossConfig.getBucketName()+"."+ossConfig.getEndpoint()+"/"+facePath;
        templatePath = "https://"+ossConfig.getBucketName()+"."+ossConfig.getEndpoint()+"/"+templatePath;
        Map map = faceConfig.compareFace(facePath, templatePath);
        return Result.success(map);
    }
    // 人脸检测与五官定位
    @ApiOperation(value = "人脸检测与五官定位")
    @PostMapping("upload/faceDetect")
    public Result<FileUploadDTO> faceDetectResult(
            @RequestParam("face") MultipartFile face,
            @RequestParam("fileType") String fileType
    ) throws Exception {
        String facePath = ossConfig.uploadFile(face, fileType);
        facePath = "https://"+ossConfig.getBucketName()+"."+ossConfig.getEndpoint()+"/"+facePath;
        Map map = faceConfig.detectFace(facePath);
        return Result.success(map);
    }
}
